package com.wchen.gdoc.generate.processor;

import com.alibaba.fastjson.JSONObject;
import com.wchen.gdoc.generate.config.ApplicationConfigProvide;
import com.wchen.gdoc.generate.config.SqlConfig;
import com.wchen.gdoc.generate.util.C3p0Util;
import com.wchen.gdoc.generate.util.StringTemplateUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


/**
 * 文档sql管理
 *
 * @author  wchen
 * @date    2019/11/11
 */
public class DocumentSqlManager {
    private static final SqlConfig SQL_CONFIG = ApplicationConfigProvide.getApplicationConfig().getSqlConfig();

    /**
     * 获取projectId
     *
     * @return
     */
    public String getProjectId() {
        String projectName = ApplicationConfigProvide.getApplicationConfig().getDocumentConfig().getProjectName();
        String sql = SQL_CONFIG.getQueryProjectIdSql();
        JSONObject sqlParams = new JSONObject();
        sqlParams.put("projectName", projectName);
        String buildSql = StringTemplateUtil.buildSql(sql,  sqlParams);
        return executeQueryId(buildSql);
    }

    /**
     * 获取mouldId
     *
     * @return
     */
    public String getMouldId(JSONObject sqlParams) {
        String projectName = ApplicationConfigProvide.getApplicationConfig().getDocumentConfig().getProjectName();
        String moduleName = ApplicationConfigProvide.getApplicationConfig().getDocumentConfig().getModuleName();
        sqlParams.put("projectName", projectName);
        sqlParams.put("moduleName", moduleName);
        String sql = SQL_CONFIG.getQueryModuleIdSql();
        String buildSql = StringTemplateUtil.buildSql(sql, sqlParams);
        return executeQueryId(buildSql);
    }

    /**
     * 获取classId
     *
     * @return
     */
    public String getClassId(JSONObject sqlParams) {
        String sql = SQL_CONFIG.getQueryClassIdSql();
        String buildSql = StringTemplateUtil.buildSql(sql, sqlParams);
        return executeQueryId(buildSql);
    }

    /**
     * 获取methodId
     *
     * @return
     */
    public String getMethodId(JSONObject sqlParams) {
        String sql = SQL_CONFIG.getQueryMethodIdSql();
        String buildSql = StringTemplateUtil.buildSql(sql,  sqlParams);
        return executeQueryId(buildSql);
    }

    /**
     * 保存class信息
     *
     * @return
     */
    public Boolean saveClassInfo(JSONObject sqlParams) {
        String sql = SQL_CONFIG.getInsertClassSql();
        String buildSql = StringTemplateUtil.buildSql(sql,  sqlParams);
        return executeSaveSql(buildSql);
    }

    /**
     * 保存接口信息
     *
     * @return
     */
    public Boolean saveMethodInfo(JSONObject sqlParams) {
        String sql = SQL_CONFIG.getInsertMethodSql();
        String buildSql = StringTemplateUtil.buildSql(sql, sqlParams);
        return executeSaveSql(buildSql);
    }

    /**
     * 执行查询id
     *
     * @param sql
     * @return
     */
    private String executeQueryId(String sql) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try{
            conn = C3p0Util.getConnection();
            st = conn.prepareStatement(sql);
            rs = st.executeQuery();
            while(rs.next()){
                return rs.getString(1);
            }
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            //释放资源
            C3p0Util.release(conn, st, rs);
        }
        return null;
    }


    /**
     * 执行保存sql
     *
     * @param sql
     * @return
     */
    private Boolean executeSaveSql(String sql) {
        Connection conn = null;
        PreparedStatement st = null;
        ResultSet rs = null;
        try{
            conn = C3p0Util.getConnection();
            st = conn.prepareStatement(sql);
            int result = st.executeUpdate();
            return result > 0;
        }catch (Exception e) {
            e.printStackTrace();
        }finally{
            //释放资源
            C3p0Util.release(conn, st, rs);
        }
        return Boolean.FALSE;
    }

}
